<?php

include 'includes/common.inc.php';

header('Content-Type: text/html; charset=utf-8');

function get_child_cats($par_id, $min_cat){
	global $system, $DBPrefix;
	$arr = array();
	$q = "SELECT cat_id FROM ".$DBPrefix."categories WHERE parent_id = ".$par_id;
	$res = mysql_query($q);
	$system->check_mysql($res, $q, __LINE__, __FILE__);
	while ($par1 = mysql_fetch_array($res)) {
		if ($par1['cat_id'] >= $min_cat) {
			$arr_childs = get_child_cats($par1['cat_id'],$par1['cat_id']);
			if (count($arr_childs) > 0)
				array_push($arr,implode(",",$arr_childs));
			array_push($arr,$par1['cat_id']);
		}
	}
	return $arr;
}


if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {

	$box = explode(",", ((isset($_REQUEST['box'])) ? $_REQUEST['box'] : '0,0,0,0'));

	$json = '{"markers":['."\n";

	$query1= "SELECT lat,lng,auc_id,title 
				FROM ".$DBPrefix."auc_attrs_vals av, ".$DBPrefix."auctions au  
				WHERE av.attr_id = 0 
				AND av.auc_id = au.id
				AND au.closed = 0
				AND av.lat BETWEEN ".$box[0]." AND ".$box[2]." 
				AND av.lng BETWEEN ".$box[1]." AND ".$box[3];

	if ($_REQUEST['ptype'] != 'all') {
		//$query1 .= " AND au.category in " . $_REQUEST['ptype'];
		$ptype = explode ( ',' , substr($_REQUEST['ptype'], 1,strlen($_REQUEST['ptype'])-2) );
		if (intval($ptype[1]) == 0)
			$query1 .= " AND au.category = " . $ptype[0];
		else {
				$child_cats = get_child_cats($ptype[0], $ptype[0]);
				if (count($child_cats)>0){
					$query1 .= " AND au.category in (";
					$query1 .= implode(",",$child_cats);
					$query1 .= ")";
				} else $query1 .= " AND au.category = " . $ptype[0];
					
		}
			
	}

		
	
	$result1 = mysql_query($query1);
	$system->check_mysql($result1, $query1, __LINE__, __FILE__);
	
	if(mysql_num_rows($result1)>0)
	{
		while ($par1 = mysql_fetch_array($result1)){

			//$exp_str = explode(",", $par1['attr_val']);


			$lng = $par1['lng'];

			$lat = $par1['lat'];

			$json.= "\n".'{'.'"id": "'.$par1['auc_id'].'",';

			$json.= '"title": "'.$par1['title'].'",';

			//$json.= '"address": "'.$par1['address'].'",';

			$json.= '"lat": "'.$lat.'",';

			$json.=  '"lon": "'.$lng.'"';

			$json.= '},';

		}

		$json = substr($json, 0,-1);

		echo  $json;

		echo '], ', "\n",  '"status": "OK"', "\n", '}';

	}
	else
	{
		echo '{"status": "false"}';
	}

}


?>